# https://www.icsip.net/vid/dianying.html
import os
import re
import shutil

import requests

from util.headers import get_headers_init_user_agent


root = 'static/城市电影网/'
if os.path.exists(root):
    shutil.rmtree(root)
os.makedirs(root)

url = 'https://www.icsip.net/vid/dianying.html'
headers = {
    'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36',
    # 'referer':'https://www.icsip.net/',
    # 'cookie':'Hm_lvt_d73e42bbbe7ef3ba1f1025699de0de81=1762777752; HMACCOUNT=11F650E2015DA2F4; Hm_lpvt_d73e42bbbe7ef3ba1f1025699de0de81=1762778395'
}
response = requests.get(url,headers=headers)
# print(response.status_code)
content = response.content.decode('utf-8')
# print(content)
# items = re.findall(r'<a class="myui-vodlist__thumb lazyload" href="/movdet/(.*?).html" title="(.*?)" data-original="https://pic.youkupic.com/upload/vod/(.*?).jpg">',content)
items = re.findall(r'<a class="myui-vodlist__thumb lazyload" href="/movdet/(.*?).html" title="(.*?)" data-original="(.*?).jpg"><span class="play hidden-xs"></span><span class="pic-text text-right">(.*?)</span></a>',content,re.S)
# print(items)
for item in items:
    # print(item)
    title = item[1]
    thumb_url = f"https://pic3.yzzyimg.online/upload/vod/{item[2]}.jpg"
    headers = get_headers_init_user_agent()
    headers.update({
        'referer': url
    })
    thumb_response = requests.get(thumb_url, headers=headers)
    with open(f"{root}/{title}.jpg", "wb") as f:
        f.write(thumb_response.content)